package de.karlNet.sshTray.gui;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.SwingUtilities;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.karlNet.sshTray.controller.TypeController;
import de.karlNet.sshTray.datasources.IChangeListener;
import de.karlNet.sshTray.datasources.IDataSource;
import de.karlNet.sshTray.datasources.LocalFile;
import de.karlNet.sshTray.model.Connection;

@Component
public class Tray implements IChangeListener {
	@Autowired
	private IDataSource connections = new LocalFile();

	@Autowired
	private ManageConnectionsWindow manageWindow;
	@Autowired
	private SettingsWindow settingsWindow = new SettingsWindow();
	
	@Autowired
	private TypeForm typeForm;
	private TrayIcon trayIcon;

	
	public void show() {
		if (SystemTray.isSupported()) {
			
			SystemTray tray = SystemTray.getSystemTray();
			Image image = Toolkit.getDefaultToolkit().getImage(
					getClass().getResource("/images/ssh16.png"));
			trayIcon = new TrayIcon(image, "SSH Menu");
			ActionListener actionListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				}
			};

			trayIcon.setImageAutoSize(true);
			trayIcon.addActionListener(actionListener);

			try {
				tray.add(trayIcon);
			} catch (AWTException e) {
				System.err.println("TrayIcon could not be added.");
			}
			this.readCon();
		} else {

			// System Tray is not supported

		}
	}
	
	@javax.annotation.PostConstruct
	public void init() {
		this.show();
	}

	public Tray() {

	}

	private void readCon() {
		PopupMenu popup = new PopupMenu();
		MenuItem defaultItem = new MenuItem("Exit");
		defaultItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		MenuItem refresh = new MenuItem("Refresh hosts");
		refresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				readCon();
			}
		});
		MenuItem manage = new MenuItem("Manage hosts");
		manage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				manageWindow.setVisible(true);
			}
		});
		MenuItem types = new MenuItem("Manage Types");
		types.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				typeForm.setVisible(true);
			}
		});
		MenuItem settings = new MenuItem("Settings");
		settings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				settingsWindow.setVisible(true);
			}
		});

		for (Connection connection : connections.readConnections()) {
			TrayMenuItem menuItem = new TrayMenuItem(connection);
			menuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					((TrayMenuItem) arg0.getSource()).getConnection().connect();
				}
			});
			popup.add(menuItem);
		}
		popup.addSeparator();
		popup.add(settings);
		popup.add(manage);
		popup.add(types);
		popup.add(refresh);
		popup.add(defaultItem);
		this.trayIcon.setPopupMenu(popup);
	}

	protected void trayClicked() {
	}

	public void dataHasBeenUpdated() {
		SwingUtilities.invokeLater(new Runnable() {
		    public void run() {
		    	readCon();
		    }
		});
	}
}
